package com.adcok.lcdns.activity.lotto;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.adcok.lcdns.activity.lotto.adapter.LottoAdLottoListAdapter;
import com.adcok.lcdns.common.BaseActivity;
import com.adcok.lcdns.common.LottoAdFullActivity;
import com.adcok.lcdns.cronos.data.CNLottoListData;
import com.adcok.lcdns.cronos.datahandler.CNBaseDataParser;
import com.adcok.lcdns.cronos.datahandler.CNBaseDataSet;
import com.adcok.lcdns.cronos.datahandler.CNResultSet;
import com.adcok.lcdns.cronos.http.CNLottoAdApiSetEx;
import com.adcok.lcdns.cronos.parser.CNLottoListParser;
import com.adcok.lcdns.util.ApplicationUtils;
import com.adcok.lcdns.util.CNCronosLog;
import com.adcok.lcdns.util.RecycleUtils;
import com.adcok.lcdns.R;

/**
 * 로또받기 목록 Activity
 */
public class LottoAdLottoListActivity extends BaseActivity{
	
	private Context mContext;
	
	/** 로또광고 목록 그리드뷰 **/
	private GridView mAdGrid;
	/** 목록없을시 텍스트뷰 **/
	private TextView mEmptyListTv;
	/** 로또광고 목록 AdapterView **/
	private LottoAdLottoListAdapter mListAdapter;
	/** 데이터셋 **/
	private ArrayList<CNBaseDataSet> mDataSetList;
	
	@Override
	protected int onGetContentViewResource() {
		return R.layout.lottoad_menu_lotto_list;
	}

	@Override
	protected void onInit() {
		buildComponnent();
	}
	
	private void buildComponnent(){
		this.mContext = this;
		
		mAdGrid = (GridView) findViewById(R.id.gridView_lotto);
		mEmptyListTv = (TextView) findViewById(R.id.emptyList_lotto);
		
		mListAdapter = new LottoAdLottoListAdapter(mContext);
		mAdGrid.setAdapter(mListAdapter);
		
		// 해당 광고 클릭시 전체보기로 로또번호와 해당 광고 순차적으로 보여준다.
		mAdGrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
					CNLottoListData data = (CNLottoListData) mDataSetList.get(position);
					Intent intent = new Intent(mContext, LottoAdFullActivity.class);
					intent.putExtra("position", position);
					Log.d("setOnItemClickListener", "position" + position);
					intent.putExtra("sendSeq", String.valueOf(data.sendSeq));
					startActivity(intent);
					finish();
			}
		});
		
		onRequestData();
	}
	
	private void parsingAdList(Element xmlObject) throws Exception{
		
		mDataSetList = new ArrayList<CNBaseDataSet>();
		
		CNBaseDataParser parser = new CNLottoListParser();
		CNResultSet resultSet = parser.parse(xmlObject);
		ArrayList<CNBaseDataSet> dataSet = null;
		
		if (resultSet != null)
		{
			dataSet = resultSet.getDataSetList();
			if (dataSet != null)
			{
				int count = dataSet.size();		
				CNCronosLog.d("DataSet.size():" + count);
				for (int i = 0; i < count; i++)
				{
					CNLottoListData data = (CNLottoListData) dataSet.get(i);
					
					mDataSetList.add(data);
				}
			}
		}
		
		mListAdapter.setDataSetList(mDataSetList);
		mListAdapter.notifyDataSetChanged();
	}
	
	private void updateUI(String rcode){
		if("RET0000".equals(rcode)){
			mAdGrid.setVisibility(View.VISIBLE);
			mEmptyListTv.setVisibility(View.GONE);
		} else if("RET0002".equals(rcode)){
			mAdGrid.setVisibility(View.GONE);
			mEmptyListTv.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public String onGetAsyncRequestUrl() {
		CNCronosLog.d("onGetAsyncRequestUrl()");
		String url = null;
		
		CNLottoAdApiSetEx MnetGlobalApiSetEx = CNLottoAdApiSetEx.getInstance();
		url = MnetGlobalApiSetEx.getLottoListUrl(mContext, ApplicationUtils.getPhoneNumber(mContext), 1, 8);
		
		CNCronosLog.d("onGetAsyncRequestUrl", "url : "+url);
		
		return url;
	}
	
	@Override
	public void onAsyncCancelRequest() 
	{
		super.hideLoadingDialog();
		CNCronosLog.d("onAsyncCancelRequest()");
	}

	@Override
	public void onAsyncDataError(Element xmlObject, String errorMessage)
	{
		super.hideLoadingDialog();
		
		NodeList rcodeNode = xmlObject.getElementsByTagName("rcode");
		String rcode = rcodeNode.item(0).getFirstChild().getNodeValue();
		
		if(!"RET0002".equals(rcode)){
			ApplicationUtils.showOkDialog(mContext, 0, getString(R.string.popup_alert), errorMessage);
		} else {
			updateUI(rcode);
		}
	}

	@Override
	public void onAsyncPreRequest()
	{
		super.showLoadingDialog();
	}

	@Override
	public void onAsyncRequestCompleted(Element xmlObject)
	{
		super.hideLoadingDialog();
		
			
		try {
				parsingAdList(xmlObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onAsyncRequestError(int errorNumber) {
		super.hideLoadingDialog();
		
		ApplicationUtils.showOkDialog(mContext,0,getString(R.string.popup_alert), ApplicationUtils.getErrorMessage(mContext, errorNumber));
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mListAdapter.recycle();
		System.gc();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		RecycleUtils.recursiveRecycle(getWindow().getDecorView()); // 사용된 뷰 메모리 해제
		System.gc(); // 가비지 컬렉션 수행
	}
}
